using System;
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;

namespace Hack.Authorization.Entities {
	/// <summary>
	/// Role for this user.
	/// </summary>
	[ActiveRecord("DF_Role")]
	public partial class RoleEntity {

		#region Fields
		private System.Guid _roleId;
		private string _roleName;
		private IList<UserEntity> _users = new List<UserEntity>();
		private ApplicationEntity _application;
		#endregion Fields

		#region Constructors
		/// <summary>
		/// Default constructor.
		/// </summary>
		public RoleEntity() {

		}

		/// <summary>
		/// Returns a new entity based in a data contract (gets from data store, then updates 
		/// data with the data contract data).
		/// </summary>
		/// <param name="role">Data Contract instance.</param>
		/// <returns>Returns the entity from the data store.</returns>
		public static RoleEntity FromContract(Role role) {
			RoleEntity entity = ActiveRecordMediator<RoleEntity>.FindByPrimaryKey(role.RoleId);
			entity.RoleName = role.RoleName;
			return entity;
		}

		/// <summary>
		/// Returns a data contract instance based in this entity.
		/// </summary>
		/// <returns>Returns the data contract instance.</returns>
		public Role ToContract() {
			Role dc = new Role();
			dc.Application = this.Application.ToContract();
			dc.RoleId = this.RoleId;
			dc.RoleName = this.RoleName;

			return dc;
		}
		#endregion Constructors

		#region Properties
		/// <summary>
		/// RoleId Property Name.
		/// </summary>
		public const string RoleIdProperty = "RoleId";
		/// <summary>
		/// Role Id.
		/// </summary>
		[PrimaryKey(PrimaryKeyType.Guid)]
		public System.Guid RoleId {
			get {
				return this._roleId;
			}
			set {
				this._roleId = value;
			}
		}

		/// <summary>
		/// RoleName Property Name.
		/// </summary>
		public const string RoleNameProperty = "RoleName";
		/// <summary>
		/// Role name.
		/// </summary>
		[Property(ColumnType = "String", NotNull = true)]
		public string RoleName {
			get {
				return this._roleName;
			}
			set {
				this._roleName = value;
			}
		}

		/// <summary>
		/// Application property name.
		/// </summary>
		public const string ApplicationProperty = "Application";
		/// <summary>
		/// Application that this role belongs to.
		/// </summary>
		[BelongsTo("ApplicationId", NotNull = true)]
		public ApplicationEntity Application {
			get {
				return this._application;
			}
			set {
				this._application = value;
			}
		}

		/// <summary>
		/// Users Property Name.
		/// </summary>
		public const string UsersProperty = "Users";
		/// <summary>
		/// Users that have this role.
		/// </summary>
		[HasAndBelongsToMany(typeof(UserEntity), ColumnRef = "RoleId", ColumnKey = "UserId", Table = "DF_UserRoles", Cascade = ManyRelationCascadeEnum.None, Lazy = true)]
		public virtual IList<UserEntity> Users {
			get {
				return this._users;
			}
			set {
				this._users = value;
			}
		}
		#endregion Properties
	}
}
